﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using OpiConsole;
using System;

namespace Example
{

    public class Command_Add : Command
    {
        public Command_Add()
            : base("add", "Adds two numbers (Usage: a b)", UsageLevel.NORMAL)
        {
        }

        public override bool Invoke(List<string> args)
        {
            if (args.Count == 2)
            {
                int output = Parser.toInt(args[0]) + Parser.toInt(args[1]);
                console.Print("Answer: " + output.ToString());
                return true;
            }

            return false;
        }
    }

    public class Command_Sin : Command
    {
        public Command_Sin()
            : base("sin", "Find the sine of an angle (Usage: angle)", UsageLevel.NORMAL)
        {
        }

        public override bool Invoke(List<string> args)
        {
            if (args.Count == 1)
            {
                double output = Math.Sin(Parser.toDouble(args[0]));
                console.Print("Answer: " + output.ToString());
                return true;
            }

            return false;
        }
    }

    public class Command_ChangeResolution : Command
    {
        public Command_ChangeResolution()
            : base("changeResolution", "Changes the resolution of the game. (Usage width height)", UsageLevel.NORMAL)
        {
        }

        public override bool Invoke(List<string> args)
        {
            if (args.Count == 2)
            {
                int width = Parser.toInt(args[0]);
                int height = Parser.toInt(args[1]);

                if (width <= 0 || height <= 0)
                {
                    console.Print("Error: Please enter a width and height of more than 0.", ConsoleLevel.FAIL);
                    return true;
                }

                ((Example)m_pGameManager).graphics.PreferredBackBufferWidth = width;
                ((Example)m_pGameManager).graphics.PreferredBackBufferHeight = height;
                ((Example)m_pGameManager).graphics.ApplyChanges();
                console.RestartedDevice();
                return true;
            }

            return false;
        }
    }

    public class Command_RemoveAllRectangles : Command
    {
        public Command_RemoveAllRectangles()
            : base("removeAllRectangles", "Removes all the current rectangles on the screen.", UsageLevel.NORMAL)
        {
        }

        public override bool Invoke(List<string> args)
        {
            ((Example)m_pGameManager).m_lRectangles.Clear();
            ((Example)m_pGameManager).GraphicsDevice.Reset();
            console.RestartedDevice();
            return true;
        }
    }

    public class Command_RemoveRectangle : Command
    {
        public Command_RemoveRectangle()
            : base("removeRectangle", "Removes a rectangle based on the ID given. (Usage: id)", UsageLevel.NORMAL)
        {
        }

        public override bool Invoke(List<string> args)
        {
            if (args.Count != 1)
                return false;

            int i = Parser.toInt(args[0]);

            if (i >= 0 && i < ((Example)m_pGameManager).m_lRectangles.Count)
            {
                ((Example)m_pGameManager).m_lRectangles.RemoveAt(i);
                return true;
            }
            else
            {
                //Amount of params correct so we send true and output our own error
                console.Print("Invalid rectangle ID", ConsoleLevel.WARN);
                return true;
            }
        }
    }

    public class Command_Retangle : Command
    {
        public Command_Retangle()
            : base("createRectangle", "Creates a cube. (Usage: x y z w h r g b)", UsageLevel.NORMAL)
        {
        }

        public override bool Invoke(List<string> args)
        {
            //Usage incorrect
            if (args.Count < 4)
                return false;

            Rectangle rect = new Rectangle(0,0,0,0);
            Color color = Color.Black;

            //We have pos and size
            if (args.Count >= 4)
            {
                rect = Parser.toRectangle(args, 0);
            }

            //We have it all
            if (args.Count >= 7)
            {
                color = Parser.toColor(args, 4, false);
            }

            Example gme = ((Example) m_pGameManager);
            gme.m_lRectangles.Add(new RectangleSprite(rect, gme.m_tBlankTexture));
            gme.m_lRectangles[gme.m_lRectangles.Count - 1].Colour = color;

            console.Print("Rectangle created ID:" + (gme.m_lRectangles.Count-1).ToString(), ConsoleLevel.NORM);
            return true;

        }
    }
}
